<template>
    <div class="cart-container">
        <div class="goods-list">

            <!-- 商品列表区域 -->
            <div class="mui-card" v-for="(item, i) in goodsList" :key="item.id">
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <mt-switch 
                        v-model="$store.getters.getGoodsSelected[item.id]"
                        @change="selectedChanged(item.id, $store.getters.getGoodsSelected[item.id])"></mt-switch>
                        <img :src="item.thumb_path" alt="">
                        <div class="info">
                            <h1>{{ item.title }}</h1>
                            <p>
                                <span class="price">￥{{ item.sell_price }}</span>
                                <!-- 如何从购物车中获取商品的数量 -->
                                <!-- 先创建一个空对象，然后循环购物车中所有商品的数据，把当前循环的商品id作为属性名，
                                 count作为属性值  得到{ id: count }-->
                                <numbox :initCount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
                                <a href="#" @click.prevent="remove(item.id, i)">删除</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 结算区域 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner checkout">
                    <div class="left">
                        <p>总计(不含运费)</p>
                        <p>
                            已勾选商品 <span style="color:red; font-weight:bold; font-size:16px">{{ $store.getters.getGoodsCountAndPrice.count }}</span> 件， 
                        总价
                        <span style="color:red; font-weight:bold; font-size:16px">￥{{ $store.getters.getGoodsCountAndPrice.total_price }}</span></p>
                    </div>
                    <mt-button type="danger">去结算</mt-button>
                </div>
            </div>
        </div>
    </div>
</template>


<script>
import numbox from '../subcom/cart_numbox.vue'
export default {
    data(){
        return {
            goodsList: []
        }
    },
    created(){
        this.getGoodsList()
    },
    methods:{
        getGoodsList(){
            // 获取到store中所有的商品的Id，拼接一个逗号分隔的字符串
            var idArr = []
            this.$store.state.cart.forEach(item => idArr.push(item.id))
            // 如果购物车中没有商品，则直接返回，不需要请求数据接口，防止报错
            if(idArr.length == 0) return
            this.$http.get('api/goods/getshopcarlist/' + idArr.join(','))
                .then(result => {
                    if(result.body.status === 0) {
                        this.goodsList = result.body.message;
                    }
                })
        },
        remove(id, index){
            if(!confirm('确认删除吗？')){
                return false;
            }
            
            // 点击删除，把商品从store中根据传递的Id删除
            // 通过index删除当前组件中的goodslist所对应下表的商品
            this.goodsList.splice(index, 1)
            this.$store.commit('removeFromCart', id)
        },
        selectedChanged(id, status){
            // 每当点击开关，把最新的开关状态同步到localstorage
            // console.log(id + '----' + status)
            this.$store.commit('updateGoodsSelected', {id, selected: status})
        }
    },
    components: {
        numbox
    }
}
</script>


<style lang="scss" scoped>
    .cart-container{
        background-color: #eee;
        overflow: hidden;
        .goods-list img{
            width: 60px;
            height: 70px;
            margin: 0 8px 0 5px;
        }
        .goods-list {
            h1{
                font-size: 13px;
            }
            .info{
                display: flex;
                flex-direction: column;
                justify-content: space-between;
                .price{
                    color: red;
                    font-weight: bold;
                }
                p{
                    margin-bottom: 0;
                    margin-top: 10px;
                }
            }
            .mui-card-content-inner{
                display: flex;
                align-items: center;
            }
        }
        .checkout{
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
    }
</style>